﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Mogre;

namespace WastedMechanics
{
    public class SoundInstance
    {
        FMOD.Channel Channel;
        public SoundEffect SoundSource;
        public float TimePlaying;
        public bool IsLoop;

        public SoundInstance(SoundEffect source, bool loop, FMOD.Channel channel)
        {
            Channel = channel;
            TimePlaying = 0;
            SoundSource = source;
            IsLoop = loop;

            Engine.SoundManager.SoundInstances.Add(this);
        }

        public float Volume
        {
            get
            {
                float result = 0;
                if (Channel != null)
                    Channel.getVolume(ref result);
                return result;
            }
            set
            {
                if (Channel != null)
                    Channel.setVolume(value);
            }
        }    
            
        public void Stop()
        {
            if (Channel != null)
            {
                Channel.stop();
                Engine.SoundManager.FreeChannel(Channel);
            }
        }

        public void SetPosition(Vector3 pos)
        {
            if (Channel != null)
            {
                FMOD.VECTOR fmodpos = new FMOD.VECTOR();                
                fmodpos.x = pos.x;
                fmodpos.y = pos.y;
                fmodpos.z = pos.z;
                FMOD.VECTOR fmodvel = new FMOD.VECTOR();
                fmodvel.x = 0;
                fmodvel.y = 0;
                fmodvel.z = 0;
                Channel.set3DAttributes(ref fmodpos, ref fmodvel);
            }
        }
    }
}
